package com.sts.common.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
/**
 * MyBatis-Plus 自动填充处理器
 *
 * <p>在实体类字段上标注 {@code @TableField(fill = ...)} 即可让本处理器生效，
 * 用于在插入或更新操作时自动填充创建时间、更新时间等公共字段。</p>
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    /**
     * 插入操作时的自动填充逻辑
     * @param metaObject 当前执行操作对应的实体对象元数据
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createdAt", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updatedAt", LocalDateTime.class, LocalDateTime.now());
    }
    /**
     * 更新操作时的自动填充逻辑
     * @param metaObject 当前执行操作对应的实体对象元数据
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updatedAt", LocalDateTime.class, LocalDateTime.now());
    }
}
